/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package units.allies;

//import map.World;

import org.newdawn.slick.SlickException;

//import sq.game.Quest;
import units.Unit;


/** Allies inherited from unit are designed to help the player
 * 
 */
public class Ally extends Unit
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//	
//	// Frame width and height of animations
//	public static int 	DEFAULT_FRAME_WIDTH = 72, DEFAULT_FRAME_HEIGHT = 72;
//	
//	// Common values (could be modified using the XML file "data.xml")
//	public static int 	ENEMY_RANGE = 300,
//						ALLY_RANGE = 300, 
//						ALLY_DISTANCE = 100,
//						MOVE_BACK = 60, 
//						COIN_AMOUNT = 1000;
//	
//	// particular unit values (could be modified using the XML file "data.xml")
//	public static int 	BOWMAN_MAX_HP = 60, BOWMAN_DAMAGE = 10, BOWMAN_RANGE_DAMAGE = 10, BOWMAN_COOLDOWN = 800,
//						SWORDMAN_MAX_HP = 40, SWORDMAN_DAMAGE = 8, SWORDMAN_RANGE_DAMAGE = 0, SWORDMAN_COOLDOWN = 200,
//						SPEARMAN_MAX_HP = 100, SPEARMAN_DAMAGE = 16, SPEARMAN_RANGE_DAMAGE = 0, SPEARMAN_COOLDOWN = 500,
//						GENERAL_MAX_HP = 140, GENERAL_DAMAGE = 30, GENERAL_RANGE_DAMAGE = 0, GENERAL_COOLDOWN = 400;
//
//	// The request to be shown in order to help the player
//	public static String REQUEST;
//
//	// Variables to see if the Allies are helping the player of if they are discovered
//	private static boolean help, discovered;
//	
//	// The type of the ally
//	public enum AllyType { BOWMAN, SWORDMAN, SPEARMAN, GENERAL }
//	
//	// The Ally Type of an instance 
//	private AllyType type;
//	
//	
//	/** Create a new Ally object. (Overload)
//     * @param type The Ally type
//     * @param width The width of the frame
//     * @param height The height of the frame
//     * @param maxHP The maximum HP of the unit
//     */
//    public Ally(AllyType type, int width, int height, int maxHP)
//    throws SlickException
//    {
//    	
//    	// Temporarily make the super class and then replace the images
//    	super(width, height, maxHP);
//        this.setSpeed(DEFAULT_SPEED);
//        this.setAllyType(type);
//        this.setOnMap(false);
//        
//    	String dirPath = "";
//    	
//    	switch (type) {
//			case BOWMAN:
//				dirPath = "assets/units/allies/bowman/";
//				this.setMaxHP(BOWMAN_MAX_HP);
//				this.setHP(BOWMAN_MAX_HP);
//				this.setDamage(BOWMAN_DAMAGE);
//				this.setRangeDamage(BOWMAN_RANGE_DAMAGE);
//				this.setMaxCoolDown(BOWMAN_COOLDOWN);
//				this.setName("Bowman");
//				this.setAttackType(Unit.AttackType.ARCHER);
//				break;
//	 		case SWORDMAN:
//	 			dirPath = "assets/units/allies/swordman/";
//				this.setMaxHP(SWORDMAN_MAX_HP);
//				this.setHP(SWORDMAN_MAX_HP);
//	 			this.setDamage(SWORDMAN_DAMAGE);
//	 			this.setRangeDamage(SWORDMAN_RANGE_DAMAGE);
//				this.setMaxCoolDown(SWORDMAN_COOLDOWN);
//				this.setName("Swordsman");
//				break;
//	  		case SPEARMAN:
//	  			dirPath = "assets/units/allies/spearman/";
//				this.setMaxHP(SPEARMAN_MAX_HP);
//				this.setHP(SPEARMAN_MAX_HP);
//	  			this.setDamage(SPEARMAN_DAMAGE);
//	  			this.setRangeDamage(SPEARMAN_RANGE_DAMAGE);
//				this.setMaxCoolDown(SPEARMAN_COOLDOWN);
//				this.setName("Spearman");
//				break;    		
//	  		case GENERAL:
//	  			dirPath = "assets/units/allies/general/";
//				this.setMaxHP(GENERAL_MAX_HP);
//				this.setHP(GENERAL_MAX_HP);
//	  			this.setDamage(GENERAL_DAMAGE);
//	  			this.setRangeDamage(GENERAL_RANGE_DAMAGE);
//				this.setMaxCoolDown(GENERAL_COOLDOWN);
//				this.setName("General");
//				break;
//    	}
//    	
//    	// Load the images and make the Unit look in a random direction
//    	this.loadImages(dirPath); 
//    	if (this.isUpgraded()) { this.loadUpgradeImages(dirPath); }
//    	this.standInRandomDirection();
//    	
//    }
//    
    /** Create a new Ally object. (Overload)
     * @param type The Ally type
     * @param width The width of the frame
     * @param height The height of the frame
     */
    public Ally(int team)
    throws SlickException
    {
    	// TODO
    	super(null, team);
    }
//    
//    /** Update the Ally's state for a frame.
//     * @param world A reference to the world.
//     * @param input The input object, used to take the keyboard inputs.
//     * @param delta Time passed since last frame (milliseconds).
//     */
//    public void update(World world, Input input, int delta) {
//    	
//    	if (this.isAlive()) {
//    		
//    		this.setCoolDown(this.getCoolDown()-delta);
//	    	if (this.getCoolDown()<0) { this.setCoolDown(0); }
//	    	this.setTempX(0);
//	    	this.setTempY(0);
//	    	
//	    	if (!this.isDieing()) {
//	    		
//	    		if (Ally.isHelping()) {
//	    		
//	    			boolean busy = false;
//	    		
//		    		for (Monster m : world.getMonsters()) {
//		    			busy |= this.checkUnitToFight(world, m,delta);
//			    		if (busy) { break; }
//		    		}
//		    		
//		    		if (!this.isAttacking() && busy) { this.move(this.getTempX(), this.getTempY(), world); }
//		    		
//		    		// if a monster is not in visibility range
//		    		if (!busy && (this.getTempX() == 0 && this.getTempY() == 0)) {
//		    				
//			    		double dx = (world.getPlayer().getX()-this.getX()<0?world.getPlayer().getUpperX():world.getPlayer().getLowerX())-this.getX();
//					    double dy = (world.getPlayer().getY()-this.getY()<0?world.getPlayer().getUpperY():world.getPlayer().getLowerY())-this.getY();
//					    double dist2 = calculateDistanceSqrd(dx,dy);  	
//					    	
//					    if (dist2 <= Unit.Squared(ALLY_RANGE) && dist2 > Unit.Squared(ALLY_DISTANCE)) {
//					    	
//					    	double dist = Math.sqrt(dist2);
//					    	// move close to the player
//					    	this.move(dx/dist*this.getSpeed()*delta,dy/dist*this.getSpeed()*delta,world);
//					    	// testing
//				    		//this.getPath(world, dx, dy);
//						  
//					    } else if (dist2 < Unit.Squared(MOVE_BACK)) {
//					    	
//					    	// the ally is too close to the player, so move away
//					    	this.move(-dx/ALLY_DISTANCE*this.getSpeed()*delta,-dy/ALLY_DISTANCE*this.getSpeed()*delta,world);
//						   	
//					    } else if (this.getHP() < this.getMaxHP()){
//					    	
//					    	busy = false;
//					    	dist2 = Unit.Squared(ENEMY_RANGE + ALLY_RANGE);
//					    	
//					    	// find a healing unit
//					    	for (Villager v : world.getVillagers()) {
//						    	if (v.isAlive() && !this.isDieing() && Villager.isHealingVillager(v)) {
//						    		double d_x = (v.getX()-this.getX()<0?v.getUpperX():v.getLowerX())-this.getX();
//						    		double d_y = v.getY()-this.getY();
//								    double d = calculateDistanceSqrd(dx,dy);
//						    		if (d < dist2) {
//						    			busy = true;
//						    			dist2 = d;
//						    			dx = d_x;
//						    			dy = d_y;
//						    		}
//						    	}
//					    	}
//					    	
//					    	if (busy && dist2 <= Unit.Squared(ALLY_RANGE)) {
//					    		
//					    		double dist = Math.sqrt(dist2);
//					    		// move close to the healer
//					    		this.move(dx/dist*this.getSpeed()*delta,dy/dist*this.getSpeed()*delta,world);
//							   	
//					    	} else {
//					    		
//					    		this.idle(delta);
//					    		
//					    	}
//					    	
//					    } else {
//					    	
//					    	this.idle(delta);
//					    	
//					    }
//					    	
//		    		}
//		    		
//		    		// makes sure the dialogue is shown only for a limited time
//			    	if (this.isDialogueShown()) {
//				     	this.setDialogueTimer(this.getDialogueTimer()-delta);
//				     	if (this.getDialogueTimer() <= 0) {
//				     		this.setDialogueTimer(DIALOG_INTERVAL);
//				     		this.hideDialogue();
//				     	}
//			    	}
//	    		
//		    	} else {
//		    		
//		    		// if the unit is neither attacking a monster nor looking for a healer, look for the player 
//		    		Player player = world.getPlayer();
//		    		
//			    	double dist2 = calculateDistanceSqrd(player.getX(), player.getY(), this.getX(), this.getY());
//			    	
//		    		if (this.getAllyType() == AllyType.GENERAL) {
//
//		    			if (dist2 <= Unit.Squared(CLOSE_PROXIMITY)) {
//				    		Item i = Item.searchItem(player,Item.ItemType.COINS);
//							if ((i != null && i.getValue() >= COIN_AMOUNT) || (Item.searchItem(this,Item.ItemType.COINS) != null)) {
//								this.setDialogue("Thank you for the Gold Coins! We are now at your service.");
//								if (i != null) { player.giveItem(i,this); }
//								Ally.setHelping(true);
//							} else {
//								this.setDialogue(REQUEST);
//							}
//			  				if (!this.isShowUnitDialogue()) { this.showDialogue(); }
//							this.setShowUnitDialogue(true);	
//						}
//				    	
//		    			// makes sure the dialogue is shown only for a limited time
//				    	if (this.isShowUnitDialogue()) {
//					     	this.setDialogueTimer(this.getDialogueTimer()-delta);
//					     	if ((this.getDialogueTimer() <= 0) || (dist2 > Unit.Squared(VISIBLITY))) {
//					     		this.setDialogueTimer(DIALOG_INTERVAL);
//					     		this.hideDialogue();
//					     	}
//				    	}
//				    	
//		    		}
//		    		
//			    	// make the villager look at the player when interacting with the player
//			    	if (dist2 <= Unit.Squared(VISIBLITY)) {
//			    		double distx = player.getX()-this.getX();
//			    		if (player.getY() < this.getLowerY()) { this.lookUp(); }
//			    		else if (distx < 0) { this.lookLeft(); }
//			    		else if (distx > 0 ) { this.lookRight(); }
//			    	} else {
//			    		this.idle(delta);
//			    	}
//		    		
//		    	}
//	    		
//	    	}
//	    	
//	    	if (this.isDieing()) {
//	        	this.checkDieing(false);
//	        }
//	    	
//    	}
//    	
//    	super.update(world, input, delta);
//        	
//    }
//    
//    /* Getter and Setters beyond this point. */
//    
//    /** Get the Ally type.
//     * @return The ally type.
//     */
//    public AllyType getAllyType() {
//		return this.type;
//	}
//
//    /** Set the Ally type.
//     * @param type The ally type.
//     */
//	private void setAllyType(AllyType type) {
//		this.type = type;
//	}
//
//	/** Checks if the allies are helping the player.
//     * @returns True if helping the player
//     */
//	public static boolean isHelping() {
//		return help;
//	}
//
//	/** Sets the allies helping the player status.
//	 * @param help Status of helping
//     */
//	public static void setHelping(boolean status) {
//		help = status;
//		if (status) { Unit.addMsgToStatus("Allies have joined "+Player.PLAYER_NAME+" quest."); }
//	}
//
//	/** Checks if the allies are helping the player.
//     * @returns True if already discovered by the player
//     */
//	public static boolean isDiscovered() {
//		return discovered;
//	}
//
//	/** Sets the allies been discovered by the player status.
//	 * @param help Status of been discovered
//     */
//	public static void setDiscovered(boolean discovered) {
//		Ally.discovered = discovered;
//		if (discovered) { Unit.addMsgToStatus(Player.PLAYER_NAME+" has discovered allies."); }
//	}
//	
}
